package qianting.news.common.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2WebMvc
@ConditionalOnMissingClass("org.springframework.cloud.gateway.filter.GlobalFilter")
public class Swagger2Config {

    private ApiInfo adminApiInfo() {
        return new ApiInfoBuilder()
                .title("浅听新闻接口文档")
                .description("本文档描述了该项目的一些接口")
                .version("1.0")
                .contact(new Contact("Eric", "https://gitee.com/shallow-listening0/qianting-news.git", "tzxa909822@gmail.com"))
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("浅听新闻接口文档")
                .description("本文档描述了该项目的一些接口")
                .version("1.0")
                .contact(new Contact("Eric", "https://gitee.com/shallow-listening0/qianting-news.git", "tzxa909822@gmail.com"))
                .build();
    }

    /**
     * 第一组：api
     * @return
     */
    @Bean
    public Docket webApiConfig() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("userId")
                .description("用户token")
                //.defaultValue(JwtHelper.createToken(1L, "admin"))
                .defaultValue("1")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());

        Docket webApi = new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                .apis(RequestHandlerSelectors.basePackage("qianting.news"))
                .paths(PathSelectors.regex("/.*"))
                .build()
                .globalOperationParameters(pars);
        return webApi;
    }

}
